### REPLICATION CODE ###
########################

# 15 January 2015
# didier.ruedin@wolfson.oxon.org

# FIGURE 1:
###################
# getting data in:
som <- read.csv("MIPEX-Koopmans-Aggregate.csv")
attach(som)

# empty plot:
plot(0, xlim=c(0,100), ylim=c(0,100), col="white", axes=FALSE, xlab="Equality of individual access", ylab="Cultural difference and groups rights", main="Citizenship Models 2002 and 2008")
abline(v=50, col="grey", lty=2); abline(h=50, col="grey", lty=2)
mtext("< Monism - Pluralism >",2, padj=-4, cex=0.8, col="darkgrey")
mtext("< Ethnic - Civic >",1, padj=4, cex=0.8, col="darkgrey")
axis(1); axis(2)

# add points:
# SOM: 2002
kop12 <- c("AT", "BE", "CH", "NL", "UK")
kop <- c("NL", "UK", "FR", "DE", "CH")
for(i in 1:length(kop)) {
  text(X2002[Country==kop12[i] & Stream =="Ethnic"], X2002[Country==kop12[i] & Stream =="Monism"], kop12[i], cex=0.7, font=1, col="darkgrey")
}
# Koopmans et al. (2012): 2002
# *50 + 50 to convert to MIPEX range
for(i in 1:length(kop12)) {
  text(X2002[Country==kop12[i] & Stream =="Kcultural"]*50+50, X2002[Country==kop12[i] & Stream =="Kindiv"]*50+50, kop12[i], font=3, cex=0.7, col="darkgrey")
}
# SOM: 2008
for(i in 1:length(kop)) {
  text(X2008[Country==kop12[i] & Stream =="Ethnic"], X2008[Country==kop12[i] & Stream =="Monism"], kop12[i], cex=0.7, font=1, col="black")
}
# Koopmans et al. (2012): 2008
# *50 + 50 to convert to MIPEX range
for(i in 1:length(kop12)) {
  text(X2008[Country==kop12[i] & Stream =="Kcultural"]*50+50, X2008[Country==kop12[i] & Stream =="Kindiv"]*50+50, kop12[i], cex=0.7, font=3, col="black")
}

# FIGURE 2:
###################

# getting data in:
load("../MIPEX 2007 - 2010/mipexkoop.2.RData")
attach(xk)               # these are the recombined MIPEX data
cy <- unique(names(xk))  # all countries
cy <- cy[c(-1,-2)]       # countries only

# empty plot:
plot(0, xlim=c(0,100), ylim=c(0,100), col="white", axes=FALSE, xlab="Equality of individual access", ylab="Cultural difference and groups rights", main="Citizenship Models")
abline(v=50, col="grey", lty=2); abline(h=50, col="grey", lty=2)
mtext("< Monism - Pluralism >",2, padj=-4, cex=0.8, col="darkgrey")
mtext("< Ethnic - Civic >",1, padj=4, cex=0.8, col="darkgrey")
axis(1); axis(2)
# add points
for(i in 1:length(cy)){
  cc <- cy[i]
  text(eval(as.name(cc))[1], eval(as.name(cc))[2], cc, col="darkgrey", cex=0.7)
}
for(i in 1:length(cy)){
  cc <- cy[i]
  text(eval(as.name(cc))[3], eval(as.name(cc))[4], cc, col="black", cex=0.7)
}

# PCA:
###################
library(psych) # more flexible PCA
# prepare data (transpose):
txk <- t(xk[-c(1,2)])[,1:4]
colnames(txk) <- c("Ethnic2007", "Monism2007", "Ethnic2010", "Monism2010")
vss(na.omit(txk)) # => minimum at 2 factors (Velicer MAP criterion minimum)

p1 <- principal(txk)
print(p1)
p2 <- principal(txk, nfactors=2)
print(p2)

# plots for each country: (not included in paper)
###################

# function for the country-by-country plots:
xkPlot <- function(cc) {
  plot(eval(as.name(cc))[1], eval(as.name(cc))[2], xlim=c(0,100), ylim=c(0,100), col="blue", type="o", axes=FALSE, xlab="Equality of individual access", ylab="Cultural difference and groups rights", main=cc)
  axis(1); axis(2)
  lines(eval(as.name(cc))[3], eval(as.name(cc))[4], xlim=c(0,100), ylim=c(0,100), col="red", type="o")
  abline(v=50, col="grey", lty=2); abline(h=50, col="grey", lty=2)
  arrows(eval(as.name(cc))[1], eval(as.name(cc))[2], eval(as.name(cc))[3], eval(as.name(cc)) [4], length=0.1, col="grey")
  legend("topleft", legend=c(2007, 2010), col=c("blue", "red"), lty=1, bty="n", cex=0.8)
  mtext("< Monism - Pluralism >",2, padj=-4, cex=0.8, col="grey")
  mtext("< Ethnic - Civic >",1, padj=4, cex=0.8, col="grey")
}

xkPlot("Armenia")
xkPlot("Australia")
xkPlot("Austria")
xkPlot("Belgium")
xkPlot("Bulgaria")
xkPlot("Canada")
xkPlot("Cyprus")
xkPlot("Czech.Republic")
xkPlot("Denmark")
xkPlot("Estonia")
xkPlot("Finland")
xkPlot("France")
xkPlot("Germany")
xkPlot("Greece")
xkPlot("Hungary")
xkPlot("Ireland")
xkPlot("Italy")
xkPlot("Japan")
xkPlot("Latvia")
xkPlot("Lithuania")
xkPlot("Luxembourg")
xkPlot("Malta")
xkPlot("Netherlands")
xkPlot("Norway")
xkPlot("Poland")
xkPlot("Portugal")
xkPlot("Romania")
xkPlot("Serbia")
xkPlot("Slovakia")
xkPlot("Slovenia")
xkPlot("South.Korea")
xkPlot("Spain")
xkPlot("Sweden")
xkPlot("Switzerland")
xkPlot("Turkey")
xkPlot("UK")
xkPlot("US")
